package com.ta.httplib.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    /**
     * Unicode 转 中文
     *
     * @param dataStr unicode
     */
    public static String unicodeToString(String dataStr) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(dataStr);
        char ch;
        while (matcher.find()) {
            //group 6728
            String group = matcher.group(2);
            //ch:'木' 26408
            ch = (char) Integer.parseInt(group, 16);
            //group1 \u6728
            String group1 = matcher.group(1);
            dataStr = dataStr.replace(group1, ch + "");
        }
        return dataStr;
    }

    /**
     * Decode 转 中文
     *
     * @param dataStr decode
     */
    public static String decodeToString(String dataStr) {
        String output = null;
        try {
            output = URLDecoder.decode(dataStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return output;
    }
}
